package com.realphim.app.adapter;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.realphim.app.ActivityDetailsPhim;
import com.realphim.app.R;
import com.realphim.app.model.ModelPhim;
import com.realphim.app.util.MyCache;

public class AdapterGridCategory extends BaseAdapter {

    public static final String TAG = "AdapterPhimCategory";

    private LayoutInflater inflater = null;

    private List<ModelPhim> listPhim;

    private int maxLenght = 0;

    private Context _context;

    public AdapterGridCategory(Context context, List<ModelPhim> listPhim) {
        this.listPhim = listPhim;
        inflater = ((Activity) context).getLayoutInflater();
        maxLenght = context.getResources()
                .getInteger(R.integer.max_item_in_row);
        _context = context;
    }

    @Override
    public int getCount() {
        int count = listPhim.size() / maxLenght;
        if (listPhim.size() > count * maxLenght) {
            count++;
        }
        return count;
    }

    @Override
    public List<ModelPhim> getItem(int position) {
        List<ModelPhim> list = new ArrayList<ModelPhim>();
        for (int i = 0; i < maxLenght; i++) {
            if (maxLenght * position + i < listPhim.size()) {
                list.add(listPhim.get(maxLenght * position + i));
            }
        }
        return list;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    private class Holder {
        List<ImageView> listImage;

        public Holder() {
            listImage = new ArrayList<ImageView>();
        }
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        View vi = convertView;
        if (vi == null) {
            if (maxLenght == 3) {
                vi = inflater.inflate(R.layout.row_grid_category_s, null);
                Holder holder = new Holder();
                holder.listImage.add((ImageView) vi
                        .findViewById(R.id.ivThumbnail1));
                holder.listImage.add((ImageView) vi
                        .findViewById(R.id.ivThumbnail2));
                holder.listImage.add((ImageView) vi
                        .findViewById(R.id.ivThumbnail3));
                vi.setTag(holder);
            } else {
                vi = inflater.inflate(R.layout.row_grid_category_l, null);
                Holder holder = new Holder();
                holder.listImage.add((ImageView) vi
                        .findViewById(R.id.ivThumbnail1));
                holder.listImage.add((ImageView) vi
                        .findViewById(R.id.ivThumbnail2));
                holder.listImage.add((ImageView) vi
                        .findViewById(R.id.ivThumbnail3));
                holder.listImage.add((ImageView) vi
                        .findViewById(R.id.ivThumbnail4));
                vi.setTag(holder);
            }

        }
        List<ModelPhim> list = getItem(position);
        Holder holder = (Holder) vi.getTag();
        for (int i = 0; i < list.size(); i++) {
            final ModelPhim phim = list.get(i);
            if (phim.thumbnailBitmap != null) {
                holder.listImage.get(i).setImageBitmap(phim.thumbnailBitmap);
            } else {
                holder.listImage.get(i).setImageResource(R.drawable.ic_app);
            }

            holder.listImage.get(i).setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    MyCache.getInstance().currentPhim = phim;
                    Intent i = new Intent(_context, ActivityDetailsPhim.class);
                    _context.startActivity(i);
                    ((Activity) _context).overridePendingTransition(
                            R.anim.slide_in_left, R.anim.slide_out_left);
                }
            });
        }

        return vi;

    }

}
